package com.spera.genutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.spera.aliyunUtil.CreateOSSClient;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.ImputStreamObj;

public class SpUtil {
	public static Map<String, Object> checkToken(String token,JedisCache jedisCache) {
		Map<String, Object> s = new HashMap<String, Object>();
		if (!ApplicationUtils.isNotNull(token)) {
			s.put(ApplicationUtils.STATUS, "0");
			s.put(ApplicationUtils.MSG, "请先登录");
			return s;
		} else {
			if (!jedisCache.exists(token)) {
				s.put(ApplicationUtils.STATUS, "-9999");
				s.put(ApplicationUtils.MSG, "用户登录已失效，请先重新登录");
				return s;
			} else{
				//刷新token
				jedisCache.expire(token, JedisCont.JEDIS_EXPRISE);
				return null;
			}
		}
	}
	
	public static BaseResult checkTokenNew(String token,JedisCache jedisCache) {
		BaseResult s = new BaseResult();
		if (!ApplicationUtils.isNotNull(token)) {
			s.setStatus("0");
			s.setMsg("请先登录");
			return s;
		} else {
			if (!jedisCache.exists(token)) {
				s.setStatus("-9999");
				s.setMsg("用户登录已失效，请先重新登录");
				return s;
			} else{
				//刷新token
				jedisCache.expire(token, JedisCont.JEDIS_EXPRISE);
				s.setStatus("1");
				s.setMsg("操作成功");
				return s;
			}
		}
	}
	
	public static String getSessionId(){
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
	    ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
	    HttpServletRequest request = sra.getRequest();
	    return request.getSession().getId();
	}
	
	public static String imageFileUpOss(String bucketName, String key, InputStream fis,Long fileSize)
            throws Exception {
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(fileSize);
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".img-cn-hangzhou.aliyuncs.com/" + key;
    }
	
	public static String mp3FileUpOss(String bucketName, String key, InputStream fis,Long fileSize)
            throws Exception {
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(fileSize);
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/" + key;
    }
	
    public static String imageFileUpOss(String bucketName, String key, File file)
            throws Exception {
    	FileInputStream fis = new FileInputStream(file);
    	OSSClient client = CreateOSSClient.getInstance().getOSSClient();
        ObjectMetadata objectMeta = new ObjectMetadata();
        objectMeta.setContentLength(file.length());
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
       
        client.putObject(bucketName, key, fis, objectMeta);
        return "http://" + bucketName + ".img-cn-hangzhou.aliyuncs.com/" + key;
    }
    
    public static InputStream getHttpInputStream(String netUrl) throws Exception{
    	URL url = new URL(netUrl);
		HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
		httpUrl.connect(); 
		return httpUrl.getInputStream();
    }
    
    public static ImputStreamObj getHttpInputStreamObj(String netUrl) throws Exception{
    	URL url = new URL(netUrl);
		HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
		httpUrl.connect(); 
		ImputStreamObj obj = new ImputStreamObj(httpUrl.getInputStream(),Long.parseLong(httpUrl.getHeaderField("Content-Length")));
		//httpUrl.disconnect();
		return obj;
    }
    
    public static ImputStreamObj getMP3URL(String surl) throws Exception {  
        String strLine;  
        Long length = 0L;
        URL url = new URL(surl);  
        HttpURLConnection objHttp = (HttpURLConnection) url.openConnection();  
  
        InputStream objIS = objHttp.getInputStream();  
        BufferedReader objReader = new BufferedReader(new InputStreamReader(objIS));  
  
        if ((strLine = objReader.readLine()) != null) {  
        	length += strLine.length();
        }  
        //objHttp.disconnect();  
        objHttp = null;  
//        objReader.close();  
        objReader = null;  
        url = null;  
        ImputStreamObj obj = new ImputStreamObj(objIS,length);
        return obj;
    }  
    
}
